package admin.forms;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import admin.data.UserData;

import models.UserModel;

public class UserForm {
	public TreeMap checkValid(HttpServletRequest request, UserModel user) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String username = (String) request.getParameter("username");
		String fullname = (String) request.getParameter("fullname");
		String status = (String) request.getParameter("status");
		String role = (String) request.getParameter("role");
		String action = (String) request.getAttribute("action");
		
		
		if (username == null || username.equals("")) {
			check.put("username", "Username is not empty");
			user.setUsername(username);
		} else {
			UserData uData = new UserData();
			if ((action == null || !action.equalsIgnoreCase("edit")) && uData.checkUserExists(username)) {
				check.put("username", "Username has been exists! Please choose other username.");
			} else {
				user.setUsername(username);
			}
		}
		
		if (fullname == null || fullname.equals("")) {
			check.put("fullname", "Full name is not empty");
			user.setFullname(fullname);
		} else {
			user.setFullname(fullname);
		}
		
		if (status != null) {
			try {
				user.setStatus(Integer.parseInt(status));
			} catch (Exception e) {
				check.put("status", "Status for user not valid!");
			}
		}
		
		if (role != null) {
			try {
				user.setRole(Integer.parseInt(role));
			} catch (Exception e) {
				check.put("role", "Role for user not valid!");
			}
		}
		
		form.put("checkValid", check);
		form.put("user", user);
		
		return form;
	}
}
